package com.byinsight.web.actions.util;

import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;

import com.byinsight.web.actions.ErrorCode;
import com.byinsight.web.actions.WebException;

abstract public class BaseXMLParser {

	public void parse(String xml) {
		SAXParser saxParser = null;
		SAXParserFactory factory = SAXParserFactory.newInstance();
		factory.setNamespaceAware(true);
		factory.setValidating(true);
		try {
			saxParser = factory.newSAXParser();
			DefaultHandler handler = getHandler();
			saxParser.parse(new InputSource(new StringReader(xml)),
					handler);
			
		} catch (Exception e) {
			throw new WebException(getParseErrorCode(), e);
		}
	}
	
	abstract protected DefaultHandler getHandler();
	
	abstract protected ErrorCode getParseErrorCode();
}
